package com.mg.axechen.room

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mg.axechen.room.bean.User
import com.mg.axechen.room.dao.UserDao
import com.mg.axechen.room.db.AppDataBase
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers

class MainActivity : AppCompatActivity() {

    private var tvAdd: TextView? = null
    private var rvList: RecyclerView? = null

    private var userList: MutableList<User> = mutableListOf()

    private val viewModel by lazy { ViewModelProvider(this).get(UserViewModel::class.java) }

    private var mDisposables: CompositeDisposable? = null

    private val listAdapter: ListAdapter by lazy {
        ListAdapter(this, userList, object : ClickListener {
            override fun deleteClick() {
                getUserList()
            }

            override fun updateClick(user: User) {
                var intent = Intent(this@MainActivity, UpdateUserActivity::class.java)
                intent.putExtra(UpdateUserActivity.INTENT_TAG_USER, user)
                startActivity(intent)
            }

        })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mDisposables = CompositeDisposable();

        initView()
        initClickListener()
        initRecycler()
        getUserListByRxJava()
    }

    private fun initView() {
        tvAdd = findViewById(R.id.tvAdd)
        rvList = findViewById(R.id.rvList)
    }

    private fun initClickListener() {
        tvAdd?.setOnClickListener {
            startActivity(Intent(this, AddUserActivity::class.java))
        }
    }

    private fun initRecycler() {
        rvList?.run {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter = listAdapter
        }
    }

    /**
     * 直接获取数据
     */
    private fun getUserList() {
        // TODO 注意这个可能要放到子线程去获取
        var getUserList = AppDataBase.getInstance(this).userDao().getUsers()
        userList.clear()
        userList.addAll(getUserList)
        listAdapter.notifyDataSetChanged()
    }

    /**
     *
     */
    private fun getUserListByRxJava() {
        var disposable = AppDataBase.getInstance(this)
            .userDao().getUsersByRxjava().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                userList.clear()
                userList.addAll(it)
                listAdapter.notifyDataSetChanged()
            }, {

            })
        mDisposables?.add(disposable)


    }

    private fun getUserListByLiveData() {
        viewModel.userLiveData.observe(this , Observer {
            userList.clear()
            userList.addAll(it)
            listAdapter.notifyDataSetChanged()
        })
    }



    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        try {
            getUserList()
        } catch (e: Exception) {
        }
    }
}
